/// ClassName [map_extends]
///
/// Description:
/// Author: 乔
/// Date: 2024/4/28
part of common_lib_plugin;

extension DetailedWhere<K, V> on Map<K, V> {
  Map<K, V> where(bool Function(K key, V value) f) => Map<K, V>.fromEntries(
        entries.where((entry) => f(entry.key, entry.value)),
      );

  Map<K, V> whereKey(bool Function(K key) f) => {...where((key, value) => f(key))};

  Map<K, V> whereValue(bool Function(V value) f) => {...where((key, value) => f(value))};

  Iterable<MapEntry<K, V>> filter(bool Function(MapEntry<K, V> entry) f) sync* {
    for (final entry in entries) {
      if (f(entry)) yield entry;
    }
  }

  Map<K, V> operator |(Map<K, V> other) => {...this}..addEntries(other.entries);
}

// void main() {
// const Map<String, int> people = {'foo': 20, 'bar': 31, 'baz': 25, 'qux': 32};
// final peopleOver30 = people.filter((e) => e.value > 30);
// print(peopleOver30); // 输出结果：(MapEntry(bar: 31), MapEntry(qux: 32))

// const userInfo = {'name': 'StellarRemi', 'age': 28};
// const address = {'address': 'shanghai', 'post_code': '200000'};
// final allInfo = userInfo | address;
// print(allInfo);
// 输出结果：{name: StellarRemi, age: 28, address: shanghai, post_code: 200000}
// }
